package com.lijin.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lijin.domain.dto.Current;
import com.lijin.domain.dto.Next;
import com.lijin.domain.json.LotteryData;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * Created by gold-jx on 2017/5/27.
 */
@Component
public class LotteryUtil {

  public static List<LotteryData> getInfo(String lotteryUrl, String code) {

    URL url;
    StringBuffer sb = new StringBuffer();
    String line = null;

    try {
      url = new URL(
          lotteryUrl);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      InputStream is = conn.getInputStream();
      BufferedReader buffer = new BufferedReader(
          new InputStreamReader(is));
      while ((line = buffer.readLine()) != null) {
        sb.append(line);
      }

      LotteryUtil lotteryUtil = new LotteryUtil();
      List<LotteryData> list = lotteryUtil.parsem(sb.toString(), code);

      return list;
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  public List<LotteryData> parsem(String json, String code) {
    if (json == null || json.length() < 1) {
      return null;
    }
    try {
      ObjectMapper mapper = new ObjectMapper();
           /* JavaType javaType = mapper.getTypeFactory()
                    .constructParametricType(List.class, LotteryData.class);*/
      // 如果是Map类型
      // dao.getTypeFactory().constructParametricType(HashMap.class,String.class,
      // Bean.class);
      Map<String, Object> map = JsonUtils.jsonStrToMap(json);
      List<LotteryData> lst = new ArrayList<>();
      if (map.get("data") == null) {
        LotteryData lotteryData = new LotteryData();
        Current52kjwang current = mapper.readValue(json, Current52kjwang.class);
        lotteryData.setOpencode(current.getCurrent().getAwardNumbers());
        lotteryData.setExpect(current.getCurrent().getPeriodNumber());
        lotteryData.setOpentime(current.getCurrent().getAwardTime());
        lst.add(0, lotteryData);
      } else {
        List<LinkedHashMap> list = (List) map.get("data");
        for (int i = 0; i < list.size(); i++) {
          LotteryData lotteryData = new LotteryData();
          lotteryData.setExpect(list.get(i).get("expect").toString());
          lotteryData.setOpencode(list.get(i).get("opencode").toString());
          lotteryData.setOpentime(list.get(i).get("opentime").toString());
          lotteryData.setOpentimestamp(list.get(i).get("opentimestamp").toString());
          lst.add(i, lotteryData);
        }
      }
           /* List<LotteryData> lst = (List<LotteryData>) dao.readValue(json1,
                    javaType);
*/

      return lst;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

@Data
class Current52kjwang {

  public String time;
  public Current current;
  public Next next;
}
